package com.aopeng.controller;

import com.aopeng.entity.AopengPicture;
import com.aopeng.mapper.AopengPictureMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

/**
 * @author 谢辉
 * @description 图片管理控制器
 * @data 2020/7/15
 */
@RequestMapping("/picture")
@Controller
public class AopengPictureController {

    @Autowired
    private AopengPictureMapper aopengPictureMapper;

    private static String HTTP_NAME = "http://119.45.187.192:8081/img/";

    /**
     * 返回至图片管理页面
     *
     * @author 谢辉
     * @date 2020/7/15 18:22
     */
    @RequestMapping(value = "/page")
    public String pictureAdministrationPage(Model model) {
        System.out.println(aopengPictureMapper.selectList(null));
        model.addAttribute("picture", aopengPictureMapper.selectList(null));
        return "admin/upload-logo";
    }

    /**
     * 跳转至图片修改页面
     *
     * @author 谢辉
     * @date 2020/7/16 16:48
     */
    @RequestMapping(value = "/update/{id}")
    public String pictureUpdatePage(@PathVariable Long id, Model model) {
        System.out.println(aopengPictureMapper.selectById(id));
        model.addAttribute("picture",aopengPictureMapper.selectById(id));
        return "admin/upload-alter";
    }

    /**
     * 根据图片id修改图片
     *
     * @author 谢辉
     * @date 2020/7/16 16:48
     */
    @RequestMapping(value = "/update/input")
    public String updatePicture(@RequestParam("id")Long id,
                                @RequestParam("pictureType")Integer pictureType,
                                @RequestParam("pictureAddress") MultipartFile  file,
                                @RequestParam("pictureName")String pictureName,
                                @RequestParam("pictureDescr")String pictureDescr,
                                @RequestParam("linkAddress")String linkAddress) throws IOException {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }

        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String path = "\\main\\resources\\static\\img\\";
        String filePath = System.getProperty("user.dir")+path;      //文件上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        AopengPicture picture = new AopengPicture(id,HTTP_NAME+fileName,pictureName,pictureType,pictureDescr,linkAddress,1L,1L,1);

        aopengPictureMapper.updateById(picture);
        return "redirect:/picture/page";
    }


    /**
     * 根据图片id删除图片
     *
     * @author 谢辉
     * @date 2020/7/16 16:48
     */
    @RequestMapping(value = "/delete/{id}/input")
    public String deletePicture(@PathVariable Long id, Model model) {
        if (id != null) {
            aopengPictureMapper.deleteById(id);
            model.addAttribute("picture", aopengPictureMapper.selectList(null));
            model.addAttribute("status", 1);
        }
        return "admin/upload-logo";
    }

    /**
     * 根据图片id查看该图片
     *
     * @author 谢辉
     * @date 2020/7/16 16:48
     */
    @RequestMapping(value = "/select/{id}/input")
    public String showPicture(@PathVariable Long id, Model model) {
        aopengPictureMapper.selectById(id);
        return "";
    }

    /**
     * 返回到图片上传form表单
     *
     * @author 谢辉
     * @date 2020/7/17 0:30
     */
    @RequestMapping(value = "/upload")
    public String uploadPicturePage() {
        return "admin/upload-input";
    }

    /**
     * 处理图片上传的方法
     *
     * @author 谢辉
     * @date 2020/7/15 18:22
     */
    @RequestMapping(value = "/upload/input",method = RequestMethod.POST)
    public String uploadFormPicture(@RequestParam("pictureType")Integer pictureType,
                                    @RequestParam("pictureAddress") MultipartFile  file,
                                    @RequestParam("pictureName")String pictureName,
                                    @RequestParam("pictureDescr")String pictureDescr,
                                    @RequestParam("linkAddress")String linkAddress) throws Exception{

        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String path = "\\src\\main\\resources\\static\\img\\";
        String filePath = System.getProperty("user.dir")+path;      //文件上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        AopengPicture picture = new AopengPicture(HTTP_NAME+fileName,pictureName,pictureType,pictureDescr,linkAddress,1L,1L,1);
        aopengPictureMapper.insert(picture);
        //String filename = "/temp-rainy/" + fileName;
        //model.addAttribute("filename", filename);

        return "redirect:/picture/page";
    }

}
